<?php
class main extends base{

	public function init(){
		parent::init();

		$this->cityObj = D('city');
	}

	public function index(){
		exit(404);
	}

	//城市列表

	public function citylist()
	{
		$cond = "status=1 and level>1 and is_site=1";
        $result = $this->cityObj->findAll(array($cond),'sort asc');
		if($result){
			foreach ($result as $key => $val) {
				$areas[$val['first']][] = $val;
			}
		}
		//区
		for($i=ord('A'); $i<=ord('Z'); $i++){
			$zm = chr($i);
			if($areas[$zm]){
				$citys[$zm] = $areas[$zm];
				$zimu[] = $zm;
			}
		}

		$data['zimu'] = $zimu;
		$data['citys'] = $citys;
		// dump($data);exit;
		$this->renderSuccess('获取成功','',$data);
		
	}

	//当前城市
	public function nowcity()
	{
		$lng = arg('lng');
		$lat = arg('lat');

		$cond = "status=1 and level>1 and is_site=1";
        $result = $this->cityObj->findAll(array($cond),'sort asc');
		if($result){
			foreach ($result as $key => $val) {
				$area_ids[] = $val['id'];
			}
			$area_ids = implode(',', $area_ids);

			if($lng && $lat){
				$cond = "status=1 and id IN({$area_ids})";
				$sort = " (ABS(lng - '{$lng}') +  ABS(lat - '{$lat}') ) asc ";

				$area = $this->cityObj->find(array($cond),$sort);

				$data['city'] = $area;
				// dump($data);exit;
				$this->renderSuccess('获取成功','',$data);
			}
			
		}

	}
	
}